package cn.chint.controller;

import com.alibaba.fastjson.JSON;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloController {

    private static final Logger logger = LogManager.getLogger(AdminController.class);

    @Value("${server.port}")
    private int port;
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("testRest")
    @ResponseBody
    public String testRest(){
        String result=null;
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.set("","");
            Map<String,Object> map = new HashMap<>();
            map.put("key","abcd");
            map.put("time",System.currentTimeMillis());
            HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(map));

            String uri ="http://127.0.0.1:8082/testapi";
            //Map  m = restTemplate.exchange(uri, HttpMethod.POST,entity,Map.class).getBody();
            Map m = restTemplate.exchange(uri, HttpMethod.POST, entity, Map.class).getBody();

            logger.info(JSON.toJSONString(m));
            return  JSON.toJSONString(m);

        }catch (Exception e){    result=e.getMessage();

        }
        logger.info(port);

        return result;
    }

    @RequestMapping("test")
    public String index(){
        return "pages/test.html";
    }

    @GetMapping("index2")
    @ResponseBody
    public String index2(){

        return "message:调用feign成功，code:true,端口："+port;
    }



    
}
